package com.sweets.look.ui.page;

import android.content.res.Resources;

import com.sweets.look.R;
import com.sweets.look.preference.PreferenceManager;
import com.sweets.look.ui.App;

public class WebPage {
    public static final String ABOUT_FILE = "about.html";
    public static final String BOOKMARK_FILE = "bookmarks.html";
    public static final String COLORFUL_PAGE_START = "<body onload=\"init()\">\n<div id=\"content\">\n";
    public static final int CSS_ABOUT_ID = 5;
    public static final int CSS_COLORFUL_ITEM_ID = 3;
    public static final int CSS_GRID_ITEM_ID = 2;
    public static final int CSS_LIST_ITEM_ID = 1;
    public static final int CSS_SEARCH_ID = 4;
    public static final String FOLDER_FILE = "folder.html";
    public static final String HISTORY_FILE = "history.html";
    public static final String HOMEPAGE_FILE = "homepage.html";
    public static final String JS_COLORFUL = "\nfunction init() {\nvar divs = document.getElementsByClassName(\"title\");\nfor(i=0;i<divs.length; i++){\ndivs[i].style.backgroundColor = getRandomColor();\n}\n}\nfunction getRandomColor(){\nreturn  \'#\' +\n(function(color){\nreturn (color +=  \'789abcdef789abcdef\'[Math.floor(Math.random()*16)])\n&& (color.length == 6) ?  color : arguments.callee(color);\n})(\'\');}";
    public static final String PAGE_END = "\n</div>\n</body>\n</html>";
    public static final String PAGE_START = "<body>\n<div id=\"content\">\n";


    public static String getGridItem(String url, String title) {
        StringBuilder sb = new StringBuilder().append("\n<div class=\"box\">\n<a href=\"")
                .append(url)
                .append("\"></a>\n<p class=\"title\">");
        String v0 = title.trim().length() >= 1 ? title.trim().substring(0, 1) : "";
        return sb.append(v0)
                .append("</p>\n<p class=\"url\">")
                .append(title)
                .append("</p>\n</div>\n").toString();
    }

    public static String getListItem(String url, String title) {
        return "\n<div class=\"box\">\n<a href=\"" + url + "\"></a>\n<p class=\"title\">" + title +
                "</p>\n<p class=\"url\">" + url + "</p>\n</div>\n";
    }

    public static String getScript(String js) {
        return "\n<script type=\"text/javascript\">\n" + js + "\n</script>\n";
    }

    public static String getSearchJs(String url) {
        return "\nfunction search(){\nif(document.getElementById(\"search_input\").value != \"\"){\nwindow.location.href = \""
                + url + "\" + document.getElementById(\"search_input\").value;" + "\ndocument.getElementById(\"search_input\").value = \"\";"
                + "\n}\nreturn false;\n}";
    }

    public static String getSearchPart(String hint, String imgPath) {
        return "\n<div class=\"search_part\">\n<a href=\"folder://\">\n<img class=\"smaller\" src=\""
                + imgPath + "\" >\n</a>\n<form onsubmit=\"return search()\" class=\"search_bar\">" +
                "\n<input type=\"submit\" id=\"search_submit\" value=\"" + hint + "\" >\n<span><input class=\"search\" type=\"text\" value=\"\" autocomplete=\"off\" id=\"search_input\" >"
                + "</span>\n</form>\n</div>\n";
    }

    public static String getWebCss(int id) {
        PreferenceManager pm = PreferenceManager.getInstance();
        Resources resources = App.getAppContext().getResources();

        int app_color = resources.getColor(pm.getNightModeEnabled() ? R.color.app_color_dark : R.color.app_color);
        int font_color = resources.getColor(pm.getNightModeEnabled() ? R.color.font_color_dark : R.color.font_color);
//      int hint_color = resources.getColor(pm.getNightModeEnabled() ? R.color.hint_color_dark2 : R.color.hint_color);

        int hint_color = resources.getColor(pm.getNightModeEnabled() ? R.color.driver_color_dark : R.color.driver_color);
        int border_bottom_color_dark = resources.getColor(pm.getNightModeEnabled() ? R.color.border_bottom_color_dark : R.color.border_bottom_color);
        String v1 = String.format("#%06X", Integer.valueOf(16777215 & app_color));
        String v6 = String.format("#%06X", Integer.valueOf(16777215 & font_color));
        String v8 = String.format("#%06X", Integer.valueOf(16777215 & hint_color));
        String v3 = String.format("#%06X", Integer.valueOf(16777215 & border_bottom_color_dark));
        String v4 = "";
        switch (id) {
            case 1: {
                return "\n* {padding: 0; margin: 0;}\nbody {background: " + v1 + "; max-width:100%;text-align: center;}\n"
                        + "#content {text-align:left;}" + "\n.box {vertical-align:middle;position:relative;display: block;padding-top:6px; padding-bottom:6px; padding-left:10px; padding-right:10px;box-shadow: 0px 1px rgba( 0, 0, 0, 0.1);}"
                        + "\n.box a {width: 100%;height: 100%;position: absolute;left: 0;top: 0;}" + "\n.title {color: "
                        + v6 + ";font-size: 1em;white-space: nowrap;padding-top:1px; padding-bottom:6px;overflow: hidden;margin:auto;text-overflow: ellipsis;-o-text-overflow: ellipsis;-ms-text-overflow: ellipsis;}"
                        + "\n.url {color: " + v8 + ";font-size: 0.75em;white-space: nowrap;overflow: hidden;margin:auto;text-overflow: ellipsis;-o-text-overflow: ellipsis;-ms-text-overflow: ellipsis;}";

            }
            case 2: {
                return "\n* {padding: 0; margin: 0;}\nbody {background: " + v1 + "; max-width:100%;text-align: left;}\n"
                        + ".box {position: relative; display: inline-block; width: 85px;height: 85px; border: 0;text-align: center;}" + "\n.title {color: "
                        + v6 + ";width:72px;line-height: 3em;height: 3em;font-size: 1.4em;white-space: nowrap;overflow: hidden;margin:auto;text-overflow: ellipsis;-o-text-overflow: ellipsis;-ms-text-overflow: ellipsis;}"
                        + ".url {height: 1.5em;line-height: 1.5em;width: 72px;color: " + v8 + ";font-size: 0.75em;white-space: nowrap;overflow: hidden;margin: auto;text-overflow: ellipsis;-o-text-overflow: ellipsis;-ms-text-overflow: ellipsis;}"
                        + "\n.box a {width: 100%;height: 100%;position: absolute;left: 0;top: 0;}";

            }
            case 3: {
                return "\n* {padding: 0; margin: 0;}\nbody {background: " + v1 + "; max-width:100%;text-align: center;}\n"
                        + "#content {padding-top:15px;}" + "\n.box {position:relative;display: inline-block;width: 100px;height: 155px;border: 0;text-align: center;}"
                        + ".title {color: #ffffff;width:95px;line-height:90px;height: 125px;font-size: 1.4em;white-space: nowrap;overflow: hidden;margin:auto;text-overflow: ellipsis;-o-text-overflow: ellipsis;-ms-text-overflow: ellipsis;border-radius:10px;border: 0px solid #bbb;background:#cccccc;}"
                        + ".url {height: 1.5em;line-height: 1.5em;width:72px;color: " + v8 + ";font-size: 0.75em;white-space: nowrap;overflow: hidden;margin: auto;text-overflow: ellipsis;-o-text-overflow: ellipsis;-ms-text-overflow: ellipsis;}"
                        + "\n.box a {width: 100%;height: 100%;position: absolute;left: 0;top: 0;}";

            }
            case 4: {
                return "\n* {padding: 0; margin: 0;}\nbody {background: " + v1 + "; max-width:100%;text-align: center;}\n"
                        + "#search_input{height:38px;width:95%;outline:none;border:none;font-size: 1em;background-color:transparent;color: "
                        + v6 + ";}" + "\nspan {display: block;overflow: hidden;padding-left:5px;vertical-align:middle;}"
                        + "\n.search_bar{display:table; vertical-align:middle;width:90%;height:38px;max-width:450px;margin:0 auto;border-bottom:1px solid "
                        + v3 + ";}" + "\n#search_submit{outline:none;height:38px;float:right;color: " + v6 +
                        ";font-size:16px;font-weight:bold;border:none;background-color:transparent;padding: 0 6px 0 6px;}"
                        + "\n.search_part{margin-bottom:10px;}" + "\nimg.smaller{width:95px;height:95px;max-width:100px;margin-bottom: 15px;}"
                        + "\n#content{position: absolute; top: 20%; width: 96%;margin: 2%;text-align: center;}"
                        + "\n.box {position:relative;display: inline-block;width: 75px;border: 0;}" + "\n.title {color: "
                        + v6 + ";width:75px;line-height: 2.4em;height: 2.4em;font-size: 1.2em;white-space: nowrap;overflow: hidden;margin:auto;text-overflow: ellipsis;-o-text-overflow: ellipsis;-ms-text-overflow: ellipsis;}"
                        + "\n.url {height: 1.5em;line-height: 1.5em;width: 72px;color: " + v8 + ";font-size: 0.75em;white-space: nowrap;overflow: hidden;margin: auto;text-overflow: ellipsis;-o-text-overflow: ellipsis;-ms-text-overflow: ellipsis;}"
                        + "\n.box a {width: 100%;height: 100%;position: absolute;left: 0;top: 0;}";

            }
            case 5: {
                return "\n* {padding: 0; margin: 0;}\nbody {background: " + v1 + "; color: " + v6 + "; font-size: 1em; max-width:100%; width:100%; max-width:800px; text-align: center; margin:auto;}\n"
                        + "\ndiv{ width:90%; max-width:800px; padding-left:5%; padding-right:5%; text-align:left;}"
                        + "\ndiv>h1{ font-size: 1.5em; padding-top:75px; padding-bottom:65px; text-align: left; }"
                        + "\ndiv>p{ text-align: left; line-height: 1.8em; text-indent:2em; }" + "\na{ color: #8968CD; font-size: 1em; text-decoration:none; }";

            }
        }

        return v4;

    }

    public static String getWebHead(String title) {
        if (title == null) {
            title = "";
        }

        return "<!DOCTYPE html>\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<meta content=\"en-us\" http-equiv=\"Content-Language\" />\n<meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\" />\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n<title>"
                + title + "</title>" + "\n</head>\n";
    }

    public static String getWebStyle(String css) {
        return "<style>" + css + "\n</style>\n";
    }
}

